package com.xiguthings.waka.shiro.matcher;

import com.xiguthings.waka.exception.HttpSendErrorException;
import com.xiguthings.waka.publicUtils.MD5Util;
import com.xiguthings.waka.publicUtils.StringUtils;
import com.xiguthings.waka.shiro.CustomizedToken;
import com.xiguthings.waka.web.controller.base.AbstractController;
import com.xiguthings.waka.web.controller.wechat.wx.WxConnExec;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;

import java.util.HashMap;
import java.util.Map;


public class CustomCredentialsMatcher extends SimpleCredentialsMatcher {

    private static final String Key_loginType_maintainer = AbstractController.Key_loginType_maintainer;
    private static final String Key_loginType_user = AbstractController.Key_loginType_user;

    /**
     * 自定义加密,或者其他的比较器
     */
    @Override
    public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
        // 1.向下转型
        CustomizedToken upToken = (CustomizedToken) token;
        String loginType = upToken.getLoginType();

        // 小程序的登录比较
        if (loginType.equals(Key_loginType_user)) {
            return true;
        }

        // web页面登陆的比较
        if (loginType.equals(Key_loginType_maintainer)) {
            char[] password = upToken.getPassword();
            if (password != null && password.length > 0) {
                // 2.将用户在界面输入的原始密码加密
                Object pwd = MD5Util.getMD5Code(new String(upToken.getPassword()));
                // 3.取出数据库中加密的密码
                Object dbPwd = info.getCredentials();
                return this.equals(pwd, dbPwd);
            }
        }


        return false;

    }
}
